<?php

class WPUF_Edit_Post {
    function __construct() {
        add_shortcode( 'wpuf_edit', array($this, 'shortcode') );
    }
    /**
     * Handles the edit post shortcode
     *
     * @return string generated form by the plugin
     */
    function shortcode() {
        ob_start();		
        if ( is_user_logged_in() && $_GET['ssaID'] != '' ) {
            $this->prepare_form();
        } else {
            printf( __( "This page is restricted. Please %s to view this page.", 'wpuf' ), wp_loginout( '', false ) );
        }
        $content = ob_get_contents();
        ob_end_clean();
        return $content;
    }
    /**
     * Main edit post form
     *
     * @global type $wpdb
     * @global type $userdata
     */
    function prepare_form() {
        global $wpdb, $userdata;
        $post_id = isset( $_GET['pid'] ) ? intval( $_GET['pid'] ) : 0;
        //is editing enabled?
        if ( wpuf_get_option( 'enable_post_edit', 'yes' ) != 'yes' ) {
            return __( 'Post Editing is disabled', 'wpuf' );
        }
        $curpost = get_post( $post_id );
        if ( !$curpost ) {
            return __( 'Invalid post', 'wpuf' );
        }
        //has permission?
        if ( !current_user_can( 'delete_others_posts' ) && ( $userdata->ID != $curpost->post_author ) ) {
            return __( 'You are not allowed to edit', 'wpuf' );
        }
        //perform delete attachment action
        if ( isset( $_REQUEST['action'] ) && $_REQUEST['action'] == "del" ) {
            check_admin_referer( 'wpuf_attach_del' );
            $attach_id = intval( $_REQUEST['attach_id'] );

            if ( $attach_id ) {
                wp_delete_attachment( $attach_id );
            }
        }
        //process post
        if ( isset( $_POST['wpuf_edit_post_submit'] ) && wp_verify_nonce( $_REQUEST['_wpnonce'], 'wpuf-edit-post' ) ) {
            $this->submit_post();
        }
        //show post form
        $this->edit_form( $curpost );
    }
	
/////////////////////////////////////////////
//////////////// EDIT BLOW //////////////////
/////////////////////////////////////////////

    function edit_form( $curpost ) {
		
		// GET THE SSA INFO
 		$ssaID = $_GET['ssaID'];
		$ssa = get_user_by('id', $ssaID);

       ?>
        <div id="wpuf-post-area">
        
            <?php
				// The Query
					$the_query = new WP_Query( 'p=' . $curpost->ID );
				// The Loop
					while ( $the_query->have_posts() ) : $the_query->the_post();
						the_content();
					endwhile;
				// Reset Post Data
					wp_reset_postdata();
			?>
            
            <p><br /><h3>The following document are need to process your shortsale:</h3></p>
            
            <ul>
            	<li>Authorization to Release Information (One for each lien on property)</li>
            	<li>Listing Agreement</li>
            	<li>Short Sale Facilitation Agreement</li>
            	<li>Agent Referral Form</li>
            	<li>Mortgage Statements For Each Lender</li>
            </ul>
            
            <p><br /><h3>You may Fax, Email OR upload your documents.</h3>
			<strong>Fax:</strong> 1-800-918-7028<br />
			<strong>Email:</strong> <?php echo $ssa->user_email; ?></p><br />
            
            <form name="wpuf_edit_post_form" id="wpuf_edit_post_form" action="" enctype="multipart/form-data" method="POST">
                <?php wp_nonce_field( 'wpuf-edit-post' ) ?>
                <ul class="wpuf-post-form">

                    <?php //do_action( 'wpuf_add_post_form_top', $curpost->post_type, $curpost ); //plugin hook      ?>
                    <?php //wpuf_build_custom_field_form( 'top', true, $curpost->ID ); ?>

                    <?php //do_action( 'wpuf_add_post_form_description', $curpost->post_type, $curpost ); ?>
                    <?php //wpuf_build_custom_field_form( 'description', true, $curpost->ID ); ?>

                    <?php //do_action( 'wpuf_add_post_form_after_description', $curpost->post_type, $curpost ); ?>
                    <?php //wpuf_build_custom_field_form( 'tag', true, $curpost->ID ); ?>

                    <?php do_action( 'wpuf_add_post_form_tags', $curpost->post_type, $curpost ); ?>
                    <?php //wpuf_build_custom_field_form( 'bottom', true, $curpost->ID ); ?>

                    <li>
                        <label>&nbsp;</label>
                        <input class="wpuf_submit" type="submit" name="wpuf_edit_post_submit" value="Send Documents to <?php echo $ssa->first_name . ' ' . $ssa->last_name; ?>">
                        <input type="hidden" name="wpuf_edit_post_submit" value="yes" />
                        <input type="hidden" name="post_id" value="<?php echo $curpost->ID; ?>">
                    </li>
                </ul>
            </form>
        </div>

        <?php
    }

    function submit_post() {
        //global $userdata;
		$post_id = $_POST['post_id'];
 		if ( $_GET['ssaID'] != '' ) { $ssaID = $_GET['ssaID']; } else { die(); }
		$ssa = get_user_by('id', $ssaID); //print_r($ssa->user_email)
		
		echo '<div class="success">' . __( 'Documents Sent to ' . $ssa->first_name . ' ' . $ssa->last_name . ' Succesfully.', 'wpuf' ) . '</div>';
		
		do_action( 'wpuf_edit_post_after_update', $post_id );
		
		// GET The post conent and store for email to the SSA
			$the_query = new WP_Query( 'p=' . $post_id );
			while ( $the_query->have_posts() ) : $the_query->the_post();
				$message = str_replace('<br>', "\r", strip_tags(get_the_content(), '<br>'));
			endwhile;
			wp_reset_postdata();
		// Get Post Attachment Links and store too
			$message .= "\rSubmitted Documents List: \r\r";
			$args = array(
				'post_type' => 'attachment',
				'numberposts' => null,
				'post_status' => null,
				'post_parent' => $post_id
			); 
			$attachments = get_posts($args);
			if ($attachments) {
				foreach ($attachments as $attachment) {
					$message .= str_replace('/home/webadmin/trudocs.com/html', 'http://trudocs.com', get_attached_file($attachment->ID)) . "\r";
					$attachmentPaths[] = get_attached_file($attachment->ID);
				}
			}
		
			$headers[] = 'From: [trudocs] <submissions@trudocs.com>';
			$headers[] = 'Bcc: submissions@trudocs.com';
			wp_mail($ssa->user_email, 'New Documents Submitted', $message, $headers, $attachmentPaths);
    }

}

$wpuf_edit = new WPUF_Edit_Post();